package org.yzh.web.util.gps;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author XJD
 */
@Component
public class LbsAmapHelper {


    private static final String AMAP_KEY = "5856f7b6a5c30ef90901ad582bf4002c";





    /**
     * 根据地址获取经纬度信息
     *
     * @param address 地址
     * @return /
     */
    public static String getLonLatByAddress(String address) {
        try {
            String url = "https://restapi.amap.com/v3/geocode/geo";
            HashMap<String, Object> map = new HashMap<>(16);
            map.put("key", AMAP_KEY);
            map.put("address", address);
            String httpResult = HttpUtil.get(url, map);
             JSONObject jsonObject = JSONUtil.parseObj(httpResult);
            return "1".equals(jsonObject.getStr("status")) ? jsonObject.getJSONArray("geocodes").getJSONObject(0).getStr("location") : "";
        } catch (Exception e) {
            return "";
        }
    }


    private HashMap<String, String> getPointsLocationMap(String location, Integer radius, Integer roadLevel) {
        HashMap<String, String> map = new HashMap<>(16);
        map.put("key", AMAP_KEY);
        map.put("location", location);
        map.put("poitype", "");
        map.put("radius", radius.toString());
        map.put("extensions", "all");
        map.put("roadlevel", roadLevel.toString());
        return map;
    }


    /**
     * 根据经纬度转换地址
     *
     * @param location
     * @return
     */
    public static String getAddressByGps(String location) {
        try {
            String url = "https://restapi.amap.com/v3/geocode/regeo";
            HashMap<String, Object> map = new HashMap<>();
            map.put("key", AMAP_KEY);
            map.put("location", location);
            String httpResult = HttpUtil.get(url, map);

            JSONObject jsonObject = JSONUtil.parseObj(httpResult);
            return jsonObject.getJSONObject("regeocode").getStr("formatted_address");
        } catch (Exception ignored) {

        }
        return "";
    }


}
